//
//  ECSystemNotiViewController.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/12.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECSystemNotiViewController.h"
#import "ECSystemNotiCell.h"
#import "ECSystemNotiModel.h"

#import "ECSystemNotiDetialVc.h"

#import "CourseModel.h"
#import "ECUIVC_CourseDetail.h"

@interface ECSystemNotiViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *listTB;
@property (nonatomic, strong) NSMutableArray *listAaary;

///总个数
@property (nonatomic, assign) int allPageNum;

@end

@implementation ECSystemNotiViewController

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.listTB];
    
    [self getDataWithURL];
    
}

/*  加载数据  */
-(void)getDataWithURL{
    [HWHttpTool requestWithMethod:@"GET"
                              url:URL_GET_SystemNotifications
                           params:nil
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              [self.listTB.mj_footer endRefreshing];//结束刷新
                              self.allPageNum = [json[@"data"][@"total"] intValue];//总个数
                              NSArray *array = json[@"data"][@"resources"];
                              [self.listAaary addObjectsFromArray: [ECSystemNotiModel mj_objectArrayWithKeyValuesArray:array]];
                              //判断数组个数,隐藏footer
                              if (_listAaary.count >= self.allPageNum) {
                                  [self.listTB.mj_footer endRefreshingWithNoMoreData];
//                                  self.listTB.mj_footer.hidden = YES;
                              }else{
                                  self.listTB.mj_footer.hidden = NO;
                              }
                              [self.listTB reloadData];
                          }
                          failure:^(NSError *error) {
                              [self.listTB.mj_footer endRefreshing];
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              DLog(@"%@",str);
                              if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                  [MBProgressHUD showTipMessageInWindow:kAlertTipOfInternetFailure];
                              }
                          }];
}




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listAaary.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indetify = @"ECSystemNotiCell";
    ECSystemNotiCell *cell = [tableView dequeueReusableCellWithIdentifier:indetify];
    if (!cell) {
        cell = [[ECSystemNotiCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetify];
    }
    [cell setContentMode:self.listAaary[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ECSystemNotiModel *model = self.listAaary[indexPath.row];
    if ([model.type isEqualToString:@"global"]) {//全局通知
        ECSystemNotiDetialVc *vc = [[ECSystemNotiDetialVc alloc]init];
        vc.batchId = model.batchId;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.type isEqualToString:@"course-deadline"]){//课程详情
        CourseModel *courseModel = [[CourseModel alloc]init];
        courseModel.modelId = model.targetId;
        ECUIVC_CourseDetail *vc = [[ECUIVC_CourseDetail alloc]init];
        vc.object = courseModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//返回高度的代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ECSystemNotiModel *model = self.listAaary[indexPath.row];
    //根据内容计算高度
    CGRect rect = [model.title boundingRectWithSize:CGSizeMake(kScreenWidth-kProportion_Width(80), MAXFLOAT)
                                                             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                          attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15*kPercentage]} context:nil];
    //再加上其他控件的高度得到cell的高度
    return MAX(kProportion_Height(216), (rect.size.height + kProportion_Height(176)));
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"Mine_emptyImage_SystemNoti"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = @"暂无任何通知";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#b3b3b3"]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

-(UITableView *)listTB{
    if (!_listTB) {
        _listTB = [[UITableView alloc]init];
        _listTB.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-kProportion_Height(138 + 12) - kNavHeight -kSafeAreaBottomHeight);
        _listTB.delegate = self;
        _listTB.dataSource = self;
        _listTB.emptyDataSetSource = self;
        _listTB.emptyDataSetDelegate = self;
        _listTB.separatorStyle = 0;
        WeakSelf(weakSelf);
//        MJRefreshBackNormalFooter MJRefreshAutoNormalFooter
        _listTB.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                [weakSelf getDataWithURL];
        }];
    }
    return _listTB;
}

-(NSMutableArray *)listAaary{
    if (!_listAaary) {
        _listAaary = [[NSMutableArray alloc]init];
    }
  
    return _listAaary;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
